package com.xiangyi.teachingsystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangyi.teachingsystem.Exception.ArgVerifyException;
import com.xiangyi.teachingsystem.dao.GroupUserMapper;
import com.xiangyi.teachingsystem.dao.UserGroupMapper;
import com.xiangyi.teachingsystem.dao.UserMapper;
import com.xiangyi.teachingsystem.entity.po.GroupUser;
import com.xiangyi.teachingsystem.service.GroupUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GroupUserServiceImpl extends ServiceImpl<GroupUserMapper, GroupUser> implements GroupUserService {
    @Autowired
    UserGroupMapper userGroupMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    GroupUserMapper groupUserMapper;

    @Override
    @Transactional
    public void addUsersToGroup(List<GroupUser> groupUsers) {
        synchronized (groupUsers) {
            for (GroupUser groupUser : groupUsers) {
                if (userGroupMapper.selectById(groupUser.getGroupId()) == null)
                    throw new ArgVerifyException("groupId参数有误");
                if (userMapper.selectById(groupUser.getUserId()) == null)
                    throw new ArgVerifyException("userId参数有误");
                groupUserMapper.insert(groupUser);
            }
        }
    }

    @Override
    public List<GroupUser> groupUsersList(Integer groupId) {
        return groupUserMapper.groupUsersList(groupId);
    }
}
